﻿using System;
using System.Collections.Generic;
using System.Web.Mvc;
using htmlHelpers.Enums;
using htmlHelpers.Html;
using htmlHelpers.Menu;

namespace htmlHelpers.Link
{
    public class SushiLinkBuilder : ISushiComponentBuilder
    {
        #region Builder Properties
        public ViewContext ViewContext { get; private set; }
        private SushiLink Component { get; set; }
        public MenuItemType ItemType { get; private set; }
        public int Position { get; set; }
        #endregion

        #region Fluent Common Setters

        /// <summary>
        /// Set the name Attribute in client Html
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        public SushiLinkBuilder SetName(string name)
        {
            this.Component.HtmlProperties.Name = name;
            return this;
        }

        public SushiLinkBuilder SetAction(String action)
        {
            this.Component.Action = action;
            return this;
        }

        public SushiLinkBuilder AddInnerHtml(String value)
        {
            this.Component.HtmlProperties.Value += value;
            return this;
        }

        /// <summary>
        /// Set Caption of the Button
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public SushiLinkBuilder SetCaption(string value)
        {
            this.Component.HtmlProperties.Value = value;
            return this;
        }

        public SushiLinkBuilder SetType (MenuItemType type)
        {
            this.ItemType = type;
            return this;
        }

        public SushiLinkBuilder AddCssClass(string value)
        {
            this.Component.cssClasses.Add(value);
            return this;
        }

        public SushiLinkBuilder AddHtmlAttribute(string key, string value)
        {
            this.Component.Attributes.Add(key, value);
            return this;
        }

        #endregion
  
        #region Constructor
        public SushiLinkBuilder(ViewContext view)
        {
            this.ViewContext = view;
            this.Component = new SushiLink();
            this.Component.HtmlProperties = new HtmlProperties(view, this.Component.GetType());
            this.Component.Active = false;
            this.ItemType = MenuItemType.Link;
            this.Component.cssClasses = new List<string>();
            this.Component.Attributes = new Dictionary<string, string>();
        }
        #endregion

        #region StringBuilders


        private String CreateSushiLink()
        {
            var tagBuilder = new TagBuilder("a");
            tagBuilder.Attributes.Add("id", this.Component.HtmlProperties.Id);
            foreach (var cssClass in this.Component.cssClasses)
            {
                tagBuilder.AddCssClass(cssClass);
            }
            foreach (var key in this.Component.Attributes.Keys)
            {
                tagBuilder.Attributes.Add(key, this.Component.Attributes[key]);
            }
            if (!String.IsNullOrEmpty(Component.HtmlProperties.Name)) tagBuilder.Attributes.Add("name", this.Component.HtmlProperties.Name);
            tagBuilder.Attributes.Add("href", this.Component.Action);
            tagBuilder.InnerHtml = this.Component.HtmlProperties.Value;
            return tagBuilder.ToString(TagRenderMode.Normal);
        }

        #endregion

        #region IHtmlString

        public override String ToString()
        {
           return  CreateSushiLink();
        }

        public String ToHtmlString()
        {
            return this.ToString();
        }
        #endregion
    }
}
